/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.company.management.controller;

import java.io.Serializable;

import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.company.management.entity.Employee;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.company.management.controller.exceptions.IllegalOrphanException;
import org.company.management.controller.exceptions.NonexistentEntityException;
import org.company.management.entity.EmployeeInfo;

/**
 *
 * @author Paner
 */
@SuppressWarnings("serial")
public class EmployeeInfoJpaController implements Serializable {

    public EmployeeInfoJpaController() {
    	emf = Persistence.createEntityManagerFactory("Management");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(EmployeeInfo employeeInfo) throws IllegalOrphanException {
        List<String> illegalOrphanMessages = null;
        Employee employeeidOrphanCheck = employeeInfo.getEmployeeid();
        if (employeeidOrphanCheck != null) {
            EmployeeInfo oldEmployeeInfoOfEmployeeid = employeeidOrphanCheck.getEmployeeInfo();
            if (oldEmployeeInfoOfEmployeeid != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Employee " + employeeidOrphanCheck + " already has an item of type EmployeeInfo whose employeeid column cannot be null. Please make another selection for the employeeid field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Employee employeeid = employeeInfo.getEmployeeid();
            if (employeeid != null) {
                employeeid = em.getReference(employeeid.getClass(), employeeid.getId());
                employeeInfo.setEmployeeid(employeeid);
            }
            em.persist(employeeInfo);
            if (employeeid != null) {
                employeeid.setEmployeeInfo(employeeInfo);
                employeeid = em.merge(employeeid);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(EmployeeInfo employeeInfo) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            EmployeeInfo persistentEmployeeInfo = em.find(EmployeeInfo.class, employeeInfo.getId());
            Employee employeeidOld = persistentEmployeeInfo.getEmployeeid();
            Employee employeeidNew = employeeInfo.getEmployeeid();
            List<String> illegalOrphanMessages = null;
            if (employeeidNew != null && !employeeidNew.equals(employeeidOld)) {
                EmployeeInfo oldEmployeeInfoOfEmployeeid = employeeidNew.getEmployeeInfo();
                if (oldEmployeeInfoOfEmployeeid != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Employee " + employeeidNew + " already has an item of type EmployeeInfo whose employeeid column cannot be null. Please make another selection for the employeeid field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (employeeidNew != null) {
                employeeidNew = em.getReference(employeeidNew.getClass(), employeeidNew.getId());
                employeeInfo.setEmployeeid(employeeidNew);
            }
            employeeInfo = em.merge(employeeInfo);
            if (employeeidOld != null && !employeeidOld.equals(employeeidNew)) {
                employeeidOld.setEmployeeInfo(null);
                employeeidOld = em.merge(employeeidOld);
            }
            if (employeeidNew != null && !employeeidNew.equals(employeeidOld)) {
                employeeidNew.setEmployeeInfo(employeeInfo);
                employeeidNew = em.merge(employeeidNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = employeeInfo.getId();
                if (findEmployeeInfo(id) == null) {
                    throw new NonexistentEntityException("The employeeInfo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            EmployeeInfo employeeInfo;
            try {
                employeeInfo = em.getReference(EmployeeInfo.class, id);
                employeeInfo.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The employeeInfo with id " + id + " no longer exists.", enfe);
            }
            Employee employeeid = employeeInfo.getEmployeeid();
            if (employeeid != null) {
                employeeid.setEmployeeInfo(null);
                employeeid = em.merge(employeeid);
            }
            em.remove(employeeInfo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<EmployeeInfo> findEmployeeInfoEntities() {
        return findEmployeeInfoEntities(true, -1, -1);
    }

    public List<EmployeeInfo> findEmployeeInfoEntities(int maxResults, int firstResult) {
        return findEmployeeInfoEntities(false, maxResults, firstResult);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	private List<EmployeeInfo> findEmployeeInfoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(EmployeeInfo.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public EmployeeInfo findEmployeeInfo(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(EmployeeInfo.class, id);
        } finally {
            em.close();
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	public int getEmployeeInfoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<EmployeeInfo> rt = cq.from(EmployeeInfo.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
